package com.two.screen.water.balance.widget.dialog

import android.annotation.SuppressLint
import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.two.screen.water.balance.R
import com.two.screen.water.balance.ui.App
import com.two.screen.water.balance.utils.DialogUtil
import com.two.screen.water.balance.widget.DividerItemDecoration
import kotlinx.android.synthetic.main.activity_index.*
import me.grantland.widget.AutofitTextView


class SelectedOneDialog(var mContext: Context) : Dialog(mContext, R.style.ChooseDialog) {

    private var mSelectedOneDialogListener: SelectedOneDialogListener? = null
    private var mTitleTv: TextView? = null
    private var mCancelTv: TextView? = null
    private var mCommonRecyclerView: RecyclerView? = null
    lateinit var mSelectedOneDialogAdapter: SelectedOneDialogAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialog_selected_one)
        this.setCanceledOnTouchOutside(false)

        val window = window
        if (window != null) {
            val attributes = window.attributes
            attributes.width = App.screenWidth
            window.attributes = attributes
            window.setGravity(Gravity.CENTER)
        }

        mCancelTv = findViewById(R.id.dialog_cancel_tv)
        mTitleTv = findViewById(R.id.dialog_title_tv)
        mCommonRecyclerView = findViewById(R.id.mCommonRecyclerView)

        mCancelTv?.setOnClickListener { dismiss() }
        initRecyclerView()
    }

    private fun initRecyclerView() {
        (mCommonRecyclerView?.itemAnimator as SimpleItemAnimator).supportsChangeAnimations = false //去闪烁
        mHadChooseRecyclerView?.addItemDecoration(DividerItemDecoration(mContext, DividerItemDecoration.HORIZONTAL_LIST, 1, ContextCompat.getColor(App.context!!, R.color.c6)))
        mSelectedOneDialogAdapter = SelectedOneDialogAdapter()
        mCommonRecyclerView?.layoutManager = LinearLayoutManager(mContext)
        mCommonRecyclerView?.adapter = mSelectedOneDialogAdapter
        mSelectedOneDialogAdapter.bindToRecyclerView(mCommonRecyclerView)
        mSelectedOneDialogAdapter.setEmptyView(R.layout.layout_status_layout_manager_empty1)
    }

    /*商品列表 适配器*/
    inner class SelectedOneDialogAdapter : BaseQuickAdapter<SelectedBean, BaseViewHolder>(R.layout.item_dialog_selected_one, null) {
        @SuppressLint("CheckResult", "NotifyDataSetChanged")
        override fun convert(helper: BaseViewHolder, item: SelectedBean) {
            helper.getView<AutofitTextView>(R.id.item_atv1).text = item.name
            var choseIv = helper.getView<ImageView>(R.id.item_choose_iv)
            /*商品图片*/
            helper.getView<View>(R.id.item_root_view).setOnClickListener {
                item.isSelected = !item.isSelected
                mSelectedOneDialogListener?.onSelected(item)
                dismiss()
            }
            /*商品 选择*/
            choseIv.setImageResource(if (item.isSelected) R.drawable.svg_choose_selected else R.drawable.svg_choose_unselected)
        }
    }

    fun setListData(list: List<SelectedBean>?) {
        mSelectedOneDialogAdapter.setNewData(list)
    }

    fun setSelectedOneDialogListener(l: SelectedOneDialogListener) {
        mSelectedOneDialogListener = l
    }


    interface SelectedOneDialogListener {
        fun onSelected(item: SelectedBean)
    }
    override fun show() {
        super.show()
        //隐藏状态栏
        window?.let { DialogUtil.hideNavigationBar(it) }
    }
}
